package org.sundatagroup.aurora.show.entity.asset;

import org.sundatagroup.aurora.show.entity.model.AssetModel;

import com.google.gson.JsonObject;

/**
 * <p>资源抽象类
 * </p>
 * 
 * @version 0.2.0
 * <br>该类修正为抽象类, 不再内部保留 data 和 type 数据
 * 
 * @author CharlesChen
 * @author Zdream
 * @date 2018-04-24
 * @since 0.1.1
 */
public abstract class Asset implements IAssetEntity {
    private int aid;
    protected String name;
    protected String desc;

    public Asset() {
    }

    public Asset(String name) {
        this.name = name;
        this.aid = 0;
        this.desc = "";
    }

    /**
     * 验证规则：aid大于0、name不为空
     * @return
     */
    public boolean isValid() {
        if (aid > 0 && name.length() != 0) {
            return true;
        } else {
            throw new IllegalArgumentException("增加的Asset参数错误，请修改！");
        }
    }

    /**
     * @return {@link Asset}转换为{@link AssetModel}
     */
    public AssetModel toModel() {
        AssetModel assetModel = new AssetModel();
        assetModel.setAid(aid);
        assetModel.setName(name);
        assetModel.setType(type().getName());

        return assetModel;
    }

    /**
     * @param am 需要转换的AssetModel
     * @return {@link AssetModel}转换为{@link Asset}
     */
    public static Asset fromModel(AssetModel am) {
        return AssetFactory.build(am);
    }

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
    
    /*
     * 不完整的
     */
    @Override
    public JsonObject toJson() {
    	JsonObject o = new JsonObject();
    	o.addProperty("aid", aid);
    	o.addProperty("type", type().getName());
		o.addProperty("name", name);
		o.addProperty("desc", desc);
		
    	return o;
    }
}
